<?php
/**
* @package		Joomla
* @subpackage	System
* @copyright	Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
* @license		GNU/GPL, see LICENSE.php
* Joomla! is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/

/* Check to ensure this file is included in Joomla! */
defined( '_JEXEC' ) or die( 'Restricted access' );
$mainframe->registerEvent( 'onBeforeDisplayContent', 'pluginContentJTags' );

	/**
	* Retrieves tags list
	*
	* @param 	object		The article object.  Note $article->text is also available
	* @param 	object		The article params
	* @param 	int			The 'page' number
	* @return	string		Tags list to be displayed
	*/
function pluginContentJTags( &$row, &$params, $page=0 )
{
	jimport('joomla.filter.output');
	$option = JRequest::getCmd( 'option' , 'null');
	$flag = 0;
	$text = '<p>';

	$plugin =& JPluginHelper::getPlugin('content', 'jtags');
	$pluginParams = new JParameter($plugin->params);

	if ($pluginParams->get('title', 1)) $text .= JText::_('Tags'). ': ';

	$tags = array();
	$tags = contentJTags_gettags($option, $row->id);
	$separator = $pluginParams->get('separator', '|');

	foreach($tags as $tag)
	{
		$formattedName = str_replace(" ", "+", substr($tag->name, 0, 20));
		if ($flag == 0) $flag = 1;
			else $text .= " ".$separator." ";
		$link = JFilterOutput::ampReplace('index.php?searchword=' .
						$formattedName . '&searchphrase=all&areas[]=jtags&option=com_search');
		if ($pluginParams->get('underline', 1))
			$text .= "<u><a href=\"$link\">".$tag->name."</a></u>";
			else $text .= "<a href=\"$link\">".$tag->name."</a>";
	}

	$text .= '</p>';
	if ($tags) return $text;
	else return;
}

	/**
	* Retrieves tags list from database
	*
	* @access	public
	* @param 	Int			$id			Item ID
	* @param 	String		$option		component
	* @return 	String		$tags		Array of tags
	* @since	1.5
	*/
function contentJTags_gettags($option, $id)
{
	$tags = array();

	$db =& JFactory::getDBO();
	$query = 'SELECT * FROM #__jtags_tags NATURAL JOIN #__jtags_items' .
			 ' WHERE component = \''.$option.'\' AND item_id = \''.$id.'\' ORDER BY `name`';
	$db->setQuery($query);
	$tags = $db->loadObjectList();

	return $tags;
}
?>